package com.sxq.fileutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import com.sxq.fileabs.SwapIo;

public class SwapSteamRead extends SwapIo
{

	@Override
	public void fileWrite(File f, String message)
	{
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		
		try
		{
			fos = new FileOutputStream(f,true);
			osw = new OutputStreamWriter(fos, "utf-8");
			
			osw.write(message);
			osw.write(LINE);
			osw.flush();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try
			{
				if (null != osw)
				{
					osw.close();
				}
				if (null != fos)
				{
					fos.close();
				}
			} catch (Exception e2)
			{
				// TODO: handle exception
			}
		}
	}

	@Override
	public String fileRead(File f)
	{
		String res = "";
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		
		try
		{
			fis = new FileInputStream(f);
			isr = new InputStreamReader(fis,"utf-8");
			br = new BufferedReader(isr);
			
			String s = "";
			while(null != (s = br.readLine()))
			{
				res += s + "\n";
			}
			
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try
			{
				if (null != br)
				{
					br.close();
				}
				if (null != isr)
				{
					isr.close();
				}
				if (null != fis)
				{
					fis.close();
				}
			} catch (Exception e2)
			{
				// TODO: handle exception
			}
		}
		return res;
	}

}
